package com.zju.DoctorGo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zju.DoctorGo.entity.Appointment;
import com.zju.DoctorGo.entity.Booking;
import com.zju.DoctorGo.mapper.AppointmentMapper;
import com.zju.DoctorGo.mapper.BookingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.math.BigInteger;
import java.util.List;

import static org.apache.ibatis.javassist.bytecode.analysis.FramePrinter.print;

@CrossOrigin
@RestController
@RequestMapping("/api/appointment") // 定义了这个控制器的基础路由
public class AppointmentController {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    BookingMapper bookingMapper;

    // 通过预约id获取预约信息
    @GetMapping("/getById")
    public Appointment getAppointmentById(@RequestParam("id") Long id) {
        return appointmentMapper.selectById(id);
    }

    // 获取某个病人的所有预约信息
    @GetMapping("/getByPatientId")
    public java.util.List<Appointment> getAppointmentsByPatientId(@RequestParam("id") Long id) {
        return appointmentMapper.getAppointmentsByPatientId(id);
    }

    // 获取某个医生的所有预约信息
    @GetMapping("/getByDoctorId")
    public List<Appointment> getAppointmentsByDoctorId(@RequestParam("doctorId") int id) {
        return appointmentMapper.selectList(new QueryWrapper<Appointment>().eq("doctor_id", id));
    }

    // 获取某天的所有预约信息
    @GetMapping("/getByTime")
    public java.util.List<Appointment> getAppointmentsByDate(@RequestParam("date") String date) {
        return appointmentMapper.selectList(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Appointment>().likeRight("appointmentTime", date));
    }

    // 创建预约信息
    @PostMapping("/create")
    public Appointment createAppointment(@RequestBody Appointment appointment) {
        appointmentMapper.insert(appointment);
        return appointment;
    }

    // 更新预约信息
    @PutMapping("/update")
    public Appointment updateAppointment( @RequestParam("id") Long id,@RequestParam("status") String status) {
        Appointment appointment = appointmentMapper.selectById(id);
        appointment.setStatus(Appointment.Status.valueOf(status));
        appointmentMapper.updateById(appointment);
        return appointment;
    }

    // 删除预约信息
    @DeleteMapping("/delete")
    public void deleteAppointment(@RequestParam("id") Long id) {
        appointmentMapper.deleteById(id);

    }

    // 获取所有预约信息
    @GetMapping("/getAll")
    public java.util.List<Appointment> getAllAppointments() {
        return appointmentMapper.selectList(null);
    }


}
